﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Infraestructure.CrossCutting.IoC;
using Infraestructure.CrossCutting.Logging;
using DistributedServices.Core.ErrorHandlers;
using System.ServiceModel;
using DistributedServices.MainModule.DTO;
using Application.MainModule.AddressManagement;

namespace DistributedServices.MainModule
{
    public partial class MainModuleService
    {
        public List<Address> GetAddressByUserId(Guid userId)
        {
            try
            {
                IAddressManagementService addressService = IoCFactory.Instance.CurrentContainer.Resolve<IAddressManagementService>();

                return addressService.FindAddressByUserId(userId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public Address GetAddressById(Guid addressId)
        {
            try
            {
                IAddressManagementService addressService = IoCFactory.Instance.CurrentContainer.Resolve<IAddressManagementService>();

                return addressService.FindAddressById(addressId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void ChangeAddress(Address address)
        {
            try
            {
                IAddressManagementService addressService = IoCFactory.Instance.CurrentContainer.Resolve<IAddressManagementService>();
                addressService.ChangeAddress(address);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void RemoveAddress(Address address)
        {
            try
            {
                IAddressManagementService addressService = IoCFactory.Instance.CurrentContainer.Resolve<IAddressManagementService>();
                addressService.RemoveAddress(address);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void AddAddress(Address address)
        {
            try
            {
                IAddressManagementService addressService = IoCFactory.Instance.CurrentContainer.Resolve<IAddressManagementService>();
                addressService.AddAddress(address);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

    }
}
